"use strict";
const path = require("path");

const resolve = dir => path.join(__dirname, dir);

module.exports = {
	// 基本路径
	publicPath: "/",
	// 输出文件目录
	outputDir: "dist",
	// 静态资源访问的路径
	assetsDir: "static",
	// eslint-loader 是否在保存的时候检查
	lintOnSave: true,
	// 生产环境是否生成 sourceMap 文件
	productionSourceMap: false,
	// webpack-dev-server 相关配置
	devServer: {
		port: process.env.VUE_PORT,
		open: true,
		overlay: {
			warnings: false,
			errors: true
		},
		before: require("./mock/mock-server.js"),
		proxy: {
			"/api": {
				/* 目标代理服务器地址 */
				target: process.env.VUE_APP_BASE_API_TARGET,
				/* 允许跨域 */
				changeOrigin: true
			}
		}
	},
	configureWebpack: {
		name: "RAN TEST",
		resolve: {
			alias: {
				"@": resolve("src")
			}
		},
		performance: {
			//入口起点的最大体积
			maxEntrypointSize: 50000000,
			//生成文件的最大体积
			maxAssetSize: 30000000
		}
	},
	chainWebpack(config) {
		// it can improve the speed of the first screen, it is recommended to turn on preload
		config.plugin("preload").tap(() => [
			{
				rel: "preload",
				// to ignore runtime.js
				fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
				include: "initial"
			}
		]);
		// when there are many pages, it will cause too many meaningless requests
		config.plugins.delete("prefetch");
		// set svg-sprite-loader
		config.module.rule("svg").exclude.add(resolve("src/icons")).end();
		config.module
			.rule("icons")
			.test(/\.svg$/)
			.include.add(resolve("src/icons"))
			.end()
			.use("svg-sprite-loader")
			.loader("svg-sprite-loader")
			.options({
				symbolId: "icon-[name]"
			})
			.end();

		config.when(process.env.NODE_ENV !== "development", config => {
			config
				.plugin("ScriptExtHtmlWebpackPlugin")
				.after("html")
				.use("script-ext-html-webpack-plugin", [
					{
						// `runtime` must same as runtimeChunk name. default is `runtime`
						inline: /runtime\..*\.js$/
					}
				])
				.end();
			config.optimization.splitChunks({
				chunks: "all",
				cacheGroups: {
					libs: {
						name: "chunk-libs",
						test: /[\\/]node_modules[\\/]/,
						priority: 10,
						chunks: "initial" // only package third parties that are initially dependent
					},
					elementUI: {
						name: "chunk-elementUI", // split elementUI into a single package
						priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
						test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
					},
					commons: {
						name: "chunk-commons",
						test: resolve("src/components"), // can customize your rules
						minChunks: 3, //  minimum common number
						priority: 5,
						reuseExistingChunk: true
					}
				}
			});
			// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
			config.optimization.runtimeChunk("single");
		});
	}
};
